package cn.qffj.memo.controller;

import cn.qffj.memo.domain.TextContent;
import cn.qffj.memo.domain.User;
import cn.qffj.memo.repository.TextContentRepositroy;
import cn.qffj.memo.repository.UserRepository;
import cn.qffj.memo.service.TextContentService;
import cn.qffj.memo.utlis.ResultVOUtils;
import cn.qffj.memo.vo.RestfulVO;
import cn.qffj.memo.vo.TextContentVO;
import cn.qffj.memo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨友奇
 * @Time 2018/5/3
 */
@RestController
@RequestMapping("textcontent")
public class TextContentRestfulController {

    @Autowired
    private TextContentService textContentService;

    //同步内容信息
    @GetMapping(value = "list/{userName}")
    public Object list(@PathVariable String userName) {
        //从用户名获取到信息
        List<TextContent> list = textContentService.findByUserName(userName);

        List<TextContentVO> voList = new ArrayList<>();

        //将多余信息过滤掉
        for (TextContent textContent : list) {
            TextContentVO vo = new TextContentVO();
            vo.setId(textContent.getId());
            vo.setText(textContent.getText());
            vo.setTime(textContent.getTime());
            vo.setFzid(textContent.getFzid());
            voList.add(vo);
        }
        return ResultVOUtils.synchronization(voList);


    }

    //数据备份
    @DeleteMapping(value = "infodata/{userName}")
    @Transactional
    public void infodata(@PathVariable String userName) {
        try {
            //删除数据库中的用户文件数据
            textContentService.deleteByUserName(userName);
        } catch (NullPointerException e) {
            System.out.println("空指针");
        }
    }

    //备份
    @PostMapping(value = "infodata")
    public RestfulVO<TextContent> infodata(TextContent textContent) {

        textContent.setText(textContent.getText());
        textContent.setTime(textContent.getTime());
        textContent.setFzid(textContent.getFzid());
        textContent.setUserName(textContent.getUserName());

        textContentService.save(textContent);

        return ResultVOUtils.backups();
    }

}
